<?php
class AccountController extends CController {

	/**
	 * @return array action filters
	 */
	public function filters() {
		return array (
			'accessControl'
		);
	}

	public function accessRules() {
		return array (
			array (
				'allow', // allow authenticated user to perform 'create' and 'update' actions
				'actions' => array (
					'changePwd',
					'changeInfo',
					'index'
				),
				'users' => array (
					'@'
				)
			),
			array (
				'allow', // allow authenticated user to perform 'create' and 'update' actions
				'actions' => array (
					'join'
				),
				'users' => array (
					'*'
				)
			),
			array (
				'deny', // deny all users
				'users' => array (
					'*'
				)
			)
		);
	}

	/**
	 * This is the default 'index' action that is invoked
	 * when an action is not explicitly requested by users.
	 */
	public function actionIndex() {
		// renders the view file 'protected/views/site/index.php'
		// using the default layout 'protected/views/layouts/main.php'
		$this->render('index', array('layout'=>$this->layout,
		'model' => new InformationForm(),
		'form' => new PasswordForm()
	));
	}

	/**
	 * Displays the contact page
	 */
	public function actionChangePwd() {
		$form = new PasswordForm;
		if (isset ($_POST['PasswordForm'])) {
			$form->attributes = $_POST['PasswordForm'];
			$form->validate();
			if (!$form->hasErrors() && $form->save()) {
				Yii::app()->user->setFlash('password', Yii::t('account', 'Password has been changed.'));
				$this->actionIndex();
				return;
			}
		}
		$this->render('changePwd', array (
			'form' => $form
		));
	}

	public function actionChangeInfo() {
		$form = new InformationForm();
		if (isset ($_POST['InformationForm'])) {
			$form->attributes = $_POST['InformationForm'];
			if ($form->validate() && $form->save()) {
				Yii::app()->user->setFlash('email', Yii::t('account', 'Your personnal informations have been changed.'));
				$this->actionIndex();
				return;
			}
		}

		$this->render('changeInfo', array (
			'model' => $form
		));
	}
	

	
	public function actionJoin() {
		echo "join";
		if (Yii::app()->user->isGuest && Yii::app()->params['allowSimulation'] === true) {
			$form = new JoinForm();
			if (isset ($_POST['JoinForm'])) {
				$form->attributes = $_POST['JoinForm'];
				if ($form->validate() && $form->save()) {
					Yii::app()->user->setFlash('email', Yii::t('account', 'Your account has been successfully created.'));
					$this->redirect(Yii::app()->homeUrl);
					return;
				}
			}
				
			$this->render('join', array (
				'model' => $form
			));
		} else {
			$this->redirect(Yii::app()->homeUrl);
		}
	}
}
?>
